package com.mdp.im.core.subscriber.action.impl;


import com.alibaba.fastjson.JSONObject;
import com.mdp.core.api.Sequence;
import com.mdp.im.core.publish.api.Publisher;
import com.mdp.im.core.scbscriber.api.action.GroupMsgAction;
import com.mdp.im.core.scbscriber.api.action.ScbscriberAction;
import com.mdp.im.core.scbscriber.api.action.PrichatMsgAction;
import com.mdp.im.core.subscriber.action.AbstractSubscriberAction;
import com.mdp.im.core.utils.msg.MsgState;
import com.mdp.im.core.utils.msg.MsgType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 新消息处理器
 * 
 * @author chenyc
 * @date 2018/5/16
 */
@Service
public class NewMessageAction extends AbstractSubscriberAction implements ScbscriberAction {
 
	@Autowired
    Publisher publisher;

	@Autowired
	Sequence seqService;
	

	@Autowired
	List<PrichatMsgAction> prichatMsgActionActions;
	

	@Autowired
	List<GroupMsgAction> groupMsgActionActions;
	
    /**
     * 新消息操作 
     * 1. 根据msgType 将消息插入不同的数据库表 prichat--> im_prichat_message, sys-->im_sys_message ,group-->im_group_message
     * 2. 将消息推送给用户。如果是群聊或者私聊，将消息按群编号转发，如果是系统消息，按defaultGroup组转发
     * 3. 发送消息回执给发送人
     * 
     * 处理消息
     * 
     * message:{ 
     *  msgId:'消息唯一编号，请保证足够随机',
     *  action:'newMessage',
     *  msgType:'群聊group|私聊prichat|系统消息sys',
     *  branchId:'归属机构号',
     *  channelId:'会话编号，后台自动补充 必填',
     *  categoryId:'群组分类编,选填',
     *  resType:'选填 相应类型CSS客服、MEM会员、PL平台', 
     *  sendUserid:'发送人编号 必填',
     *  sendUsername:'发送人姓名 必填', 
     *  sendTime:'消息时间戳，长整数类型 必填',
     *  toUserid:'接收人，msgType为prichat时必填',
     *  atUserid:'at接收人，msgType为group时选填，其它类型不用填',
     * 	groupId:'群组编号,当msgType为group时必填，msgType为prichat时，后台自动按{sendUserid}_{toUserid}排序后组成groupId',
	 *  sendObject:'发送对象，特殊需求扩展，选填，L门店P商品K规格O订单S物流T其他M手机号码W微信号码M邮箱A广告',
	 *  sendContent:'消息内容',
	 *  msgClass:'消息分类0普通消息(包括私聊及群消息)1群组通知公告2IM通知3广告CSS客服',
	 *  msgAction:'消息动作一般不用填写，特殊情况使用,inviteJoinFrend邀请成为好友,inviteJoinGroup邀请加入群,invitePhone邀请发送手机号码,inviteWeixin邀请发送微信,inviteResume邀请发送简历,inviteInterview邀请面试,showAd展示广告'
     * }
     */
    @Override
    public void doAction(JSONObject message) {
        JSONObject json = message;
        String groupId=json.getString("groupId");
        String msgId=json.getString("msgId");
        if(StringUtils.isEmpty(msgId)) {
        	msgId=seqService.getTablePK("message", "msgId");
        	message.put("msgId", msgId);
        }
        String channelId=json.getString("channelId");  
        String msgType=json.getString("msgType");
        

        switch(msgType) { 
	        case MsgType.PRICHAT:{
	        	//系统消息特殊处理
	        	String toUserid=json.getString("toUserid");
	        	String sendUserid=json.getString("sendUserid");
	        	this.prichatMsgActionActions(message);
	        	Long sendCount=super.handleMessageWithUserids(json,toUserid); 
	        	//回执处理
	        	JSONObject rm=null;
	        	String msgState= MsgState.SEND;
	        	if(sendCount>0) {
	        		msgState= MsgState.REACH;
	        		rm=this.getMessageReceipt(groupId, toUserid,sendUserid, msgId,seqService.getTablePK("message", "msgId"), msgType, msgState);
		        	//super.handleMessageReceipt(rm, channelId);
		        	publisher.publishPrichatMessage(sendUserid,rm.toJSONString());
	        	} 
	        	
	             
	        	break;
	        }
	        case MsgType.GROUP:{
	        	
	        	String sendUserid=json.getString("sendUserid");
	        	this.groupMsgActionActions(json);
	        	String msgState= MsgState.SEND;
	        	Long sendCount=super.handleMessageWithGroup(json,false,groupId); 
	        	//回执处理
	        	JSONObject rm=null; 
	        	if(sendCount>0) {
	        		msgState= MsgState.REACH;
	        		rm=this.getMessageReceipt(groupId, sendUserid,sendUserid, msgId,seqService.getTablePK("message", "msgId"), msgType, msgState);
		        	publisher.publishGroupMessage(groupId, rm.toJSONString());
	        	} 
	        	
	        	//super.handleMessageReceipt(rm, channelId);
	             
	        	break;
	        }
	        case MsgType.SYS:{
	        	//系统消息特殊处理
	        	super.handleMessageWithDefaultGroup(json,false); 
	        	String sendUserid=json.getString("sendUserid");
				//回执处理
				JSONObject rm=this.getMessageReceipt(groupId, "",sendUserid, msgId,seqService.getTablePK("message", "msgId"), msgType, MsgState.REACH);
				publisher.publishPrichatMessage(sendUserid,rm.toJSONString());
				break;
	        }
	         
        }
    }
    

    public void prichatMsgActionActions(JSONObject message) {
    	String msgAction=message.getString("msgAction");
    	if(StringUtils.isEmpty(msgAction)) {
    		return;
    	}
    	if(prichatMsgActionActions!=null && prichatMsgActionActions.size()>0) {
    		for (PrichatMsgAction msgActionAction : prichatMsgActionActions) {
    			if(msgActionAction.suportMsgAction(msgAction)) {
    				msgActionAction.doAction(message);
    			}
    			
			}
    	}
    }
    
    public void groupMsgActionActions(JSONObject message) {
    	String msgAction=message.getString("msgAction");
    	if(StringUtils.isEmpty(msgAction)) {
    		return;
    	}
    	if(groupMsgActionActions!=null && groupMsgActionActions.size()>0) {
    		for (GroupMsgAction msgActionAction : groupMsgActionActions) {
    			if(msgActionAction.suportMsgAction(msgAction)) {
    				msgActionAction.doAction(message);
    			}
    			
			}
    	}
    }
}
